<template>
	<view class="shop-action bg-color-pageBg">
		<view class="shop-action-container flex flex-js-sb flex-ai-center">
			<view class="price font-color-textPrimary flex flex-ai-center">
				<w-icon  size="19"  :name="crystalIcon(info.crystal_type)"></w-icon>
				<text class="font-size-20  font-w-500">{{info.crystal_number}}</text>
			</view>
			<view class="" v-if="exchangeStatus==1">
				<view class="action-btn flex flex-dir-col flex-center no-sub" v-if="!info.is_sub" @click="targetSub">
					<text>设置购买提醒</text>
					<u-count-down :time="countDown" format="HH:mm:ss"></u-count-down>
				</view>
				<view class="action-btn flex flex-dir-col flex-center" v-else @click="targetSub">
					<text>取消提醒</text>
					<text>
						<u-count-down :time="countDown" format="HH:mm:ss"></u-count-down>
					</text>
				</view>
			</view>
			
			<w-button v-if="exchangeStatus==2" style='width:480rpx' shape="circle" @click='buyAction'>立即兑换
			</w-button>
			<w-button v-if="exchangeStatus==4" style='width:480rpx' shape="circle" :disabled="true">已兑完</w-button>
			<w-button v-if="exchangeStatus==5" style='width:480rpx' shape="circle" :disabled="true">水晶不足</w-button>
		</view>
		
		
		<u-popup :show="show" mode='bottom' bgColor='transparent' @close="show=false">
			<view class="submit-order bg-color-item pa-12">
				<view class="flex flex-js-sb flex-ai-center">
					<view class="flex font-color-textPrimary flex flex-ai-end">
						<text>兑换数量</text>
						<text class="font-size-10 ml-4 font-color-666" v-if="maxCount">（兑换总数不得超过{{maxCount}}个）</text>
						<text class="font-size-10 ml-4 font-color-666" v-else>您已达到限购数量</text>
					</view>
					<w-icon size="24" name="close2" @click='show=false'></w-icon>
				</view>
				<view class="pt-12 flex">
					<image class="br-4" :src="$ossUrl+product.listimg" style="width: 136rpx;height: 136rpx;"></image>
					<view class=" pl-12 ptb-10 flex-1">
						<view class="price font-color-textPrimary flex flex-ai-center">
							<w-icon :name="crystalIcon(info.crystal_type)" size="24"></w-icon>
							<text class="font-size-20 ml-4">{{product.crystal.crystal_price}}</text>
						</view>
						<text class="font-size-10 font-color-textPrimary" v-if="product.goods_type==2">编号随机</text>
					</view>
					<view class="flex flex-ai-center" style="height: 61rpx;border-radius: 100rpx;border: 1rpx solid #666;">
						<view class="pa-10"><w-icon :name="count<=1? 'minus-disable':'minus-active'" size="26" @click='minus'></w-icon></view>
						<input v-model="count" type="number" @input="changeCount" class="plr-4 font-color-textPrimary"
							style="width: 126rpx;text-align: center;border-left: 1rpx solid #666;border-right: 1rpx solid #666;" />
						<view class="pa-10"><w-icon :name="count==maxCount? 'add-disable':'add-active'" size="26" @click='add'></w-icon></view>
					</view>
				</view>
				<w-button class='mt-12' shape="circle" :disabled="!count||count>maxCount"
					@click='comfirmOrder'>立即购买</w-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import crystalMixin from '@/mixins/crystalMixin.js'
	import {
		subscribe
	} from '@/api/subscribe/index.js'
	export default {
		mixins:[crystalMixin],
		props: {
			info: {
				type: Object,
				defaul() {
					return {
					
					}
				}
			}
		},
		data(){
			return {
				show:false,
				count:0,
				timer:null,
			}
		},
		computed:{
			product(){
				return this.info.product
			}
		},
		methods:{
			targetSub() {
				let data = {
					type:Number(this.info.type)+20,
					opr_id: this.info.id
				}
				subscribe(data, !this.info.is_sub).then(res => {
					uni.showToast({
						title: res.msg
					})
					this.info.is_sub=!this.info.is_sub
					this.$emit("subChange",  !this.info.is_sub)
				})
			},
			buyAction(){
				this.count=this.maxCount
				this.show=true;
			},
			changeCount({
				detail
			}) {
				let value = Number(detail.value)
				if (this.timer) clearTimeout(this.timer);
				this.timer = setTimeout(() => {
					this.count = value
					if (value < 1) {
						this.count = 1
					}
					if (value > this.maxCount) {
						this.count = this.maxCount;
					}
				}, 500)
			},
			minus() {
				if (this.count <= 1) return;
				this.count--
			},
			add() {
				if (this.count == this.maxCount) return;
				this.count++
			},
			comfirmOrder(){
				let _this=this;
				uni.setStorage({
					key:'exchange',
					data:{
						count:this.count,
						info:this.info
					},
					success() {
						_this.show=false
						uni.navigateTo({
							url:'/pages/shop/exchange'
						})
						
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.shop-action{
		width: 750rpx;
		position: fixed;
		left: 0;
		bottom: 0;
		padding-bottom: env(safe-area-inset-bottom);
		.submit-order {
			width: 750rpx;
			@extend .br-t-12,
			.br-16;
			padding-bottom: calc(env(safe-area-inset-bottom) + 24rpx);
		}
		
		
		.shop-action-container {
			@extend .plr-16,.ptb-8,.flex;
			height: 120rpx;
			.price {
				font-family: HarmonyOS Sans SC-Medium, HarmonyOS Sans SC;
				font-weight: 500;
			}
		
			.action-btn.no-sub {
				background: linear-gradient(180deg, #434976 0%, #232744 100%);
				;
				@extend .font-color-white;
				border: none;
		
				/deep/ .u-count-down__text {
					@extend .font-size-12,
					.font-color-white;
				}
			}
		
			.action-btn {
				width: 480rpx;
				height: 92rpx;
				@extend .border-color-dark,
				.font-color-dark;
				border: 1px solid;
				border-radius: 66rpx;
		
				/deep/ .u-count-down__text {
					@extend .font-size-12,
					.font-color-dark;
				}
			}
		}
		
	}
</style>